home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / brailler-04b-c / brlr ƒ / Shell ƒ / apple events.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  3.3 KB  |  124 lines  |  [TEXT/MMCC]

  1. #include "AppleEvents.h"
  2. #include "EPPC.h"
  3. #include "apple events.h"
  4. #include "generic open.h"
  5. #include "error.h"
  6.  
  7. static OSErr GotRequiredParameters(const AppleEvent *theAppleEvent);
  8. pascal OSErr HandleOAppEvent(const AppleEvent *theEvent, const AppleEvent *reply, long refCon);
  9. pascal OSErr HandleDocEvent(const AppleEvent *theEvent, const AppleEvent *reply, long refCon);
  10. pascal OSErr HandleQuitEvent(const AppleEvent *theEvent, const AppleEvent *reply, long refCon);
  11.  
  12. static    AEEventHandlerUPP gHandleOAppUPP;
  13. static    AEEventHandlerUPP gHandleDocUPP;
  14. static    AEEventHandlerUPP gHandleQuitUPP;
  15.  
  16. static OSErr GotRequiredParameters(const AppleEvent *theAppleEvent)
  17. {
  18.     OSErr myErr;
  19.     DescType returnedType;
  20.     Size actualSize;
  21.     
  22.     myErr = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard, &returnedType,
  23.                 nil, 0, &actualSize);
  24.     if (myErr == errAEDescNotFound)
  25.         return noErr;
  26.     else if (myErr == noErr)
  27.         return errAEParamMissed;
  28.     else
  29.         return myErr;
  30. }
  31.  
  32. pascal OSErr HandleOAppEvent(const AppleEvent *theEvent, const AppleEvent *reply, long refCon)
  33. {
  34.     #pragma unused(reply, refCon)
  35.     
  36.     OSErr theError;
  37.     
  38.     theError = GotRequiredParameters(theEvent);
  39.     return theError;
  40. }
  41.  
  42. pascal OSErr HandleDocEvent(const AppleEvent *theEvent, const AppleEvent *reply, long refCon)
  43. {
  44.     #pragma unused(theEvent, reply, refCon)
  45.     
  46.     OSErr theError;
  47.     AEDescList docList;
  48.     long itemsInList;
  49.     long index;
  50.     AEKeyword keyword;
  51.     DescType returnedType;
  52.     FSSpec theFileSpec;
  53.     Size actualSize;
  54.     
  55.     
  56.     theError = AEGetParamDesc(theEvent, keyDirectObject, typeAEList,  &docList);
  57.     if (theError == noErr)
  58.     {
  59.         theError = GotRequiredParameters(theEvent);
  60.         if (theError == noErr)
  61.         {
  62.             theError = AECountItems(&docList, &itemsInList);
  63.             if (theError == noErr)
  64.             {
  65.                 for (index = 1; index <= itemsInList; index++)
  66.                 {
  67.                     theError = AEGetNthPtr(&docList, index, typeFSS, &keyword, &returnedType,
  68.                                 (Ptr) &theFileSpec, sizeof(theFileSpec), &actualSize);
  69.                     if (theError == noErr)
  70.                     {
  71.                         if (refCon == kAEOpenDocuments)
  72.                             OpenTheFile(&theFileSpec);
  73.                         else
  74.                             PrintTheFile(&theFileSpec);
  75.                     };
  76.                 };
  77.             };
  78.         };
  79.         (void) AEDisposeDesc(&docList);
  80.     };
  81.     return theError;
  82. }
  83.  
  84. pascal OSErr HandleQuitEvent(const AppleEvent *theEvent, const AppleEvent *reply, long refCon)
  85. {
  86.     #pragma unused(reply, refCon)
  87.     
  88.     OSErr theError;
  89.     
  90.     theError = GotRequiredParameters(theEvent);
  91.     if (theError == noErr)
  92.     {
  93. //        PrepareToQuit();
  94. //        if (!gDone)
  95. //            theError = userCanceledErr;
  96.     };
  97.     return theError;
  98. }
  99.  
  100. OSErr InstallRequiredAppleEvents(void)
  101. {
  102.     OSErr result;
  103.     
  104.     gHandleOAppUPP = NewAEEventHandlerProc(HandleOAppEvent);
  105.     FailNilUPP((UniversalProcPtr) gHandleOAppUPP);
  106.     gHandleDocUPP = NewAEEventHandlerProc(HandleDocEvent);
  107.     FailNilUPP((UniversalProcPtr) gHandleDocUPP);
  108.     gHandleQuitUPP = NewAEEventHandlerProc(HandleQuitEvent);
  109.     FailNilUPP((UniversalProcPtr) gHandleQuitUPP);
  110.  
  111.     result = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
  112.                 gHandleOAppUPP, 0, false);
  113.     if (result == noErr)
  114.         result = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
  115.                     gHandleDocUPP, kAEOpenDocuments, false);
  116.     if (result == noErr)
  117.         result = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
  118.                     gHandleDocUPP, kAEPrintDocuments, false);
  119.     if (result == noErr)
  120.         result = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  121.                     gHandleQuitUPP, 0, false);
  122.     return result;
  123. }
  124.